/*
 * ZDTDriver.h
 *
 *  Created on: Nov 18, 2023
 *      Author: 26090
 */

#ifndef MODULES_INC_ZDTDRIVER_H_
#define MODULES_INC_ZDTDRIVER_H_
#include "stdbool.h"
#include "usart.h"
//#include "bsp_uart.h"

/**********************************************************
 ***	Emm_V5.0�����ջ���������
 ***	��д���ߣ�ZHANGDATOU
 ***	����֧�֣��Ŵ�ͷ�ջ��ŷ�
 ***	�Ա����̣�https://zhangdatou.taobao.com
 ***	CSDN���ͣ�http s://blog.csdn.net/zhangdatou666
 ***	qq����Ⱥ��262438510
 **********************************************************/

#define		ABS(x)		((x) > 0 ? (x) : -(x))
#define 	DIR(x)		((x) > 0 ? (CW): (CCW))

typedef enum
{
	S_VER = 0, /* ��ȡ�̼��汾�Ͷ�Ӧ��Ӳ���汾 */
	S_RL = 1, /* ��ȡ��ȡ���������� */
	S_PID = 2, /* ��ȡPID���� */
	S_VBUS = 3, /* ��ȡ���ߵ�ѹ */
	S_CPHA = 5, /* ��ȡ����� */
	S_ENCL = 7, /* ��ȡ�������Ի�У׼��ı�����ֵ */
	S_TPOS = 8, /* ��ȡ���Ŀ��λ�ýǶ� */
	S_VEL = 9, /* ��ȡ���ʵʱת�� */
	S_CPOS = 10, /* ��ȡ���ʵʱλ�ýǶ� */
	S_PERR = 11, /* ��ȡ���λ�����Ƕ� */
	S_FLAG = 13, /* ��ȡʹ��/��λ/��ת״̬��־λ */
	S_Conf = 14, /* ��ȡ�������� */
	S_State = 15, /* ��ȡϵͳ״̬���� */
	S_ORG = 16, /* ��ȡ���ڻ���/����ʧ��״̬��־λ */
	S_PULSE =17,
} SysParams_t;

typedef enum
{
	ZDT_ERROR = 0x00,
	ZDT_PULSE = 0x32,
	ZDT_VEL = 0x35,
	ZDT_CPOS = 0x36,
} ZDTCommand_t;

typedef enum
{
	ZDT_OK,
	ZDT_ID_ERROR,
	ZDT_COMMAND_ERROR
}ZDTReturn_t;

typedef enum
{
	CW=0,
	CCW=1
}ZDTDir_e;

typedef struct
{
	int32_t Pulse;
	int32_t CurrentPosition;
	int16_t CurrentSpeed;
} ZDTState_t;

typedef struct
{
	uint8_t id;
	ZDTState_t state;
	uint8_t received_flag;
} ZDTInstance;

typedef struct
{
	ZDTInstance *instance;
	SysParams_t Param;
	
} ZDTSend;

/**********************************************************
 ***	ZDT.0�����ջ���������
 ***	��д���ߣ�ZHANGDATOU
 ***	����֧�֣��Ŵ�ͷ�ջ��ŷ�
 ***	�Ա����̣�https://zhangdatou.taobao.com
 ***	CSDN���ͣ�http s://blog.csdn.net/zhangdatou666
 ***	qq����Ⱥ��262438510
 **********************************************************/

/**********************************************************
 *** ע�⣺ÿ�������Ĳ����ľ���˵��������Ķ�Ӧ������ע��˵��
 **********************************************************/
//static void ZDT_Reset_CurPos_To_Zero(uint8_t addr); // ����ǰλ������
//static void ZDT_Reset_Clog_Pro(uint8_t addr); // �����ת����
//static void ZDT_Read_Sys_Params(uint8_t addr, SysParams_t s); // ��ȡ����
//static void ZDT_Modify_Ctrl_Mode(uint8_t addr, bool svF, uint8_t ctrl_mode); // ���������޸Ŀ���/�ջ�����ģʽ
//static void ZDT_En_Control(uint8_t addr, bool state, bool snF); // ���ʹ�ܿ���
//static void ZDT_Vel_Control(uint8_t addr, uint8_t dir, uint16_t vel, uint8_t acc, bool snF); // �ٶ�ģʽ����
//static void ZDT_Pos_Control(uint8_t addr, uint8_t dir, uint16_t vel, uint8_t acc, uint32_t clk, bool raF, bool snF); // λ��ģʽ����
//static void ZDT_Stop_Now(uint8_t addr, bool snF); // �õ������ֹͣ�˶�
//static void ZDT_Synchronous_motion(uint8_t addr); // �������ͬ����ʼ�˶�
//static void ZDT_Origin_Set_O(uint8_t addr, bool svF); // ���õ�Ȧ��������λ��
//static void ZDT_Origin_Modify_Params(uint8_t addr, bool svF, uint8_t o_mode, uint8_t o_dir, uint16_t o_vel,
//		uint32_t o_tm, uint16_t sl_vel, uint16_t sl_ma, uint16_t sl_ms, bool potF); // �޸Ļ������
//static void ZDT_Origin_Trigger_Return(uint8_t addr, uint8_t o_mode, bool snF); // �������������
//static void ZDT_Origin_Interrupt(uint8_t addr); // ǿ���жϲ��˳�����

/********�Ŵ�ͷ�Դ�������*********/
void ZDT_Reset_CurPos_To_Zero(ZDTInstance *zdt);
void ZDT_Read_Sys_Params(ZDTInstance *zdt, SysParams_t s);
void ZDT_En_Control(ZDTInstance *zdt, bool state, bool snF);
void ZDT_Set_Sys_PID(ZDTInstance *zdt, bool save,uint32_t kp,uint32_t ki,uint32_t kd);
void ZDT_Vel_Control(ZDTInstance *zdt, uint8_t dir, uint16_t vel, uint8_t acc, bool snF);
void ZDT_Stop_Now(ZDTInstance *zdt, bool snF);
void ZDT_Pos_Control(ZDTInstance *zdt, uint8_t dir, uint16_t vel, uint8_t acc, uint32_t clk, bool raF, bool snF);
void ZDT_Stop_Now(ZDTInstance *zdt, bool snF);
void ZDT_Synchronous_motion(ZDTInstance *zdt);

void ZDT_Init(UART_HandleTypeDef *uart);
void ZDTGetPos(uint16_t id);
void ZDTGetVel(uint16_t id);

void ZDT_ReceivePro();


extern ZDTInstance zdt0,zdt1;

#endif /* MODULES_INC_ZDTDRIVER_H_ */
